#include "stdafx.h"
#include "ButtonExample.h"

FCView* ButtonExample::createView(xmlNodePtr node, const String& type){
    if (type == L"mybutton")
   {
       return new MyButton();
   }
   return FCUIXml::createView(node, type);
}

void ButtonExample::loadFile(const String& fileName, FCView *view){
    FCUIXml::loadFile(fileName, view);
    //获取第二页
    FCTabPage *tabPage = getTabPage(L"TabPage2");
    FCButton *button = new FCButton();
    FCPoint point = {100,100};
    button->setLocation(point);
    FCSize size = {100,20};
    button->setSize(size);
    tabPage->addView(button);
    button->setText(L"确定");

    //从Xml中获取按钮
    FCButton *button1 = getButton(L"Button");
    FCButton *button2 = getButton(L"Button2");
    FCButton *button3 = getButton(L"Button3");
    FCButton *button4 = getButton(L"Button4");

    //绑定事件
    button1->addEvent((FCTouchEventCallBack*)this, FCEventID_Click, this);
    button2->addEvent((FCTouchEventCallBack*)this, FCEventID_Click, this);
    button3->addEvent((FCTouchEventCallBack*)this, FCEventID_Click, this);
    button4->addEvent((FCTouchEventCallBack*)this, FCEventID_Click, this);
    button->addEvent((FCTouchEventCallBack*)this, FCEventID_Click, this);
}

void ButtonExample::callTouchEvent(String eventName, Object sender, FCTouchInfo touchInfo, Object invoke){
    FCButton *button = (FCButton*)sender;
}

MyButton::MyButton(){
    m_timerID = FCView::getNewTimerID();
    m_tick = 0;
    setBorderColor(FCColor_None);
    setTextColor(FCColor::rgb(255, 255, 255));
    FCFont tFont(L"Default", 14, true, false, false);
    setFont(&tFont);
    setBackColor(FCColor::rgb(150, 150, 150));
    setCornerRadius(4);
}

MyButton::~MyButton(){
    stopTimer(m_timerID);
}

void MyButton::onRemove(){
    FCButton::onRemove();
    stopTimer(m_timerID);
}

void MyButton::onTouchEnter(FCTouchInfo touchInfo){
    FCButton::onTouchEnter(touchInfo);
    m_tick = 60;
    startTimer(m_timerID, 10);
}

void MyButton::onTimer(int timerID){
    FCButton::onTimer(timerID);
   if (m_tick > 0)
   {
       m_tick--;
       if (m_tick == 0)
       {
           stopTimer(m_timerID);
       }
       invalidate();
   }
}

void MyButton::onPaint(FCPaint *paint, const FCRect& clipRect){
    String text = getText();
    if ((int)text.length() >= 0)
    {
        int width = getWidth(), height = getHeight();
        if (width > 0 && height > 0)
        {
            FCRect drawRect = {1, 1, width - 1, height - 1};
            if (m_tick > 0 || this == m_native->getHoveredView())
            {
                paint->fillRoundRect(FCColor::rgba(42, 138, 195, 135 + 120 * (60 - m_tick) / 60), drawRect, getCornerRadius());
            }
            else
            {
                paint->fillRoundRect(getPaintingBackColor(), drawRect, getCornerRadius());
            }

            FCFont *font = getFont();
            FCSize tSize = paint->textSize(text, font, -1);
            FCRect tRect = {(width - tSize.cx) / 2, (height - tSize.cy) / 2, (width - tSize.cx) / 2, (height + tSize.cy) / 2};
            paint->drawText(text, getPaintingTextColor(), font, tRect);
        }
    }
}
